package com.pethospital.entity;

import java.util.List;

/**
 * 分页结果实体类
 * Page Result Entity Class
 * 
 * @author PetHospital System
 * @version 1.0
 */
public class PageResult<T> {
    
    private List<T> data;           // 数据列表
    private int currentPage;        // 当前页码
    private int pageSize;           // 每页大小
    private long totalCount;        // 总记录数
    private int totalPages;         // 总页数
    private boolean hasNext;        // 是否有下一页
    private boolean hasPrevious;    // 是否有上一页

    /**
     * 默认构造函数
     */
    public PageResult() {
    }

    /**
     * 带参构造函数
     */
    public PageResult(List<T> data, int currentPage, int pageSize, long totalCount) {
        this.data = data;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.totalPages = (int) Math.ceil((double) totalCount / pageSize);
        this.hasNext = currentPage < totalPages;
        this.hasPrevious = currentPage > 1;
    }

    // Getter和Setter方法
    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
        this.hasNext = currentPage < totalPages;
        this.hasPrevious = currentPage > 1;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        this.totalPages = (int) Math.ceil((double) totalCount / pageSize);
        this.hasNext = currentPage < totalPages;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
        this.totalPages = (int) Math.ceil((double) totalCount / pageSize);
        this.hasNext = currentPage < totalPages;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public boolean isHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    /**
     * 获取起始记录号
     */
    public int getStartRecord() {
        return (currentPage - 1) * pageSize + 1;
    }

    /**
     * 获取结束记录号
     */
    public int getEndRecord() {
        int end = currentPage * pageSize;
        return (int) Math.min(end, totalCount);
    }

    /**
     * 获取分页信息文本
     */
    public String getPageInfo() {
        if (totalCount == 0) {
            return "暂无数据";
        }
        return String.format("第 %d-%d 条，共 %d 条记录，第 %d/%d 页", 
                           getStartRecord(), getEndRecord(), totalCount, currentPage, totalPages);
    }

    /**
     * 判断是否为空结果
     */
    public boolean isEmpty() {
        return data == null || data.isEmpty();
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalCount=" + totalCount +
                ", totalPages=" + totalPages +
                ", hasNext=" + hasNext +
                ", hasPrevious=" + hasPrevious +
                ", dataSize=" + (data != null ? data.size() : 0) +
                '}';
    }
}
